/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ACH.mod.Snippet;
import org.json.JSONObject;

/**
 *
 * @author adriancruz11c
 */
public class DistanceCalculator {
    public static final Number UNKNOWN_DISTANCE = -1.0;
    protected final static double logN = Math.log(1.2e12);// (1000000000000)
    
    
    	/**
	 * Calculates the normalized Google Distance (NGD) between the two terms
	 * specified.  NOTE: this number can change between runs, because it is
	 * based on the number of web pages found by Google, which changes.
	 * @return a number from 0 (minimally distant) to 1 (maximally distant),
	 *   unless an exception occurs in which case, it is negative
	 *   (RefactoringConstants.UNKNOWN_DISTANCE)
	 */
    /*
        public static Double getNGD(JSONObject t1, JSONObject t2, JSONObject par){
            Double ngd=0.0;
            //mandar aqui el total ::::
            calculateDistance(t1,t2,par);
            return ngd;
        }*/
	public static Double getNGD(JSONObject t1, JSONObject t2, JSONObject par){ //(String term1, String term2) {
		double distance = UNKNOWN_DISTANCE.doubleValue();
                //ACH
            Search se=new Search();                     
            /*
            String a[];
            String urlString;                
            
             a=term2.split(" ");
            if(a.length >1) 
                term2="\""+term2+"\"";
                */
		try {
			double min = 0.0+se.totalResults(t1);//se.numResultsFromWeb(term1); 
			double max = 0.0+se.totalResults(t2);//se.numResultsFromWeb(term2);
			double both = 0.0+se.totalResults(par);//se.numResultsFromWeb(term1 + " " + term2);

			// if necessary, swap the min and max
                       // System.out.println("logN:"+logN);
                       // System.out.println(""+ (((1.0e12)/1000000)/1000000)/2 );
                        System.out.print("\tmin:"+min+" max:"+max+" both:"+both);
			if (max < min) {
				double temp = max;
				max = min;
				min = temp;
			}

			if (min > 0.0 && both > 0.0) {
				distance =
					(Math.log(max) - Math.log(both)) / (logN - Math.log(min));
                                if (distance>1.0)
                                    distance=1.0;//ACH
			} else {
				distance = 1.0;
			}
			
			// Counts change and are estimated, so there would be a possibility
			// of a slightly negative distance.
			if (distance < 0.0) {
				distance = 0.0;
			}
		} catch (Exception e) {
			distance = UNKNOWN_DISTANCE.doubleValue();
		}
                System.out.println("\tdis:"+distance);
		return distance;
	}
        public static double getCooc(JSONObject t1, JSONObject t2, JSONObject parL,JSONObject parR){//(String t1, String t2){            
            Double r1=0.1,r2=0.1, interR1=0.1, interR2=0.1,dif=0.1,raz1=0.1, raz2=0.1,abs=0.1,sig=0.1,cooc=0.1;            
            Search se=new Search();  
            try
            {
            r1=0.0+se.totalResults(t1); //r1=Double.parseDouble(""+ se.numResultsFromWeb_exact("", t1));
            r2=0.0+se.totalResults(t2);//r2=Double.parseDouble(""+ se.numResultsFromWeb_exact("", t2));
            interR1=0.0+se.totalResults(parL);//interR1=Double.parseDouble(""+ se.numResultsFromWeb_exact("\""+t2+"\"", t1));
            interR2=0.0+se.totalResults(parR);//interR2=Double.parseDouble(""+ se.numResultsFromWeb_exact("\""+t1+"\"", t2));
            Double max=r1, min=r2;
            if (max < min) {
		double temp = max;
		max = min;
		min = temp;
            }            
            
            if(interR1>min){interR1=min;}
            if(interR2>min){interR2=min;}            
            raz1=((interR1+interR2)/2)/r1;
            raz2=((interR1+interR2)/2)/r2;
            System.out.print("  r1="+r1+" r2="+r2+ " R1="+ interR1+ " R2="+ interR2+" MIN="+min);
            dif=Math.abs(raz1-raz2);            
            dif=dif>1?1:dif<0?0:dif;            
            sig=1-dif;
            cooc=(sig+Math.sin(sig)+(0.3/2))/2;
            System.out.println("\nrz1="+raz1+" rz2="+raz2+ " dif="+dif+ " sig=" +sig+" cooc="+cooc);            
            }catch(Exception e){
                return 0.0075;
            }                       
            return cooc;
        }
        /*public Double[] calculateDistACH(String term1, String[] termsX) {
            double distance = UNKNOWN_DISTANCE.doubleValue();
            Search se=new Search();  
            Double rTotal[]=new Double[termsX.length];
            Double tot=1.0;
            Double rest=0.0;
            
            String a[];
            String urlString;
            try
            {
                tot=0.0;
                rest=0.0;
                for(int i=0; i< termsX.length; i++){
                    if(term1.equals(termsX[i])){
                    rTotal[i]=1.0;  
                    //rest=rest+1.0;
                    }else{
                  //rTotal[i] = se.numResultsFromWeb("\""+term1+"\" \""+termsX[i]+"\"");    
             
             //a=termsX[i].split(" ");
            //if(a.length >1)                
              //      rTotal[i] = se.numResultsFromWeb(""+term1+" "+termsX[i]+"");                  
            //else
                    rTotal[i] = Double.parseDouble(""+ se.numResultsFromWeb(""+term1+" "+termsX[i]+" "));                  
                  tot=tot+rTotal[i];
                    }
                }
               // tot=tot-rest;
                for(int i=0; i< termsX.length; i++){
                   if (rTotal[i]!=1.0)
                       rTotal[i]=rTotal[i]/tot;
                }
            
            } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rTotal;
        }*/

}
